package com.rcw.commonlib.fragment;


import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rcw.commonlib.R;
import com.rcw.popup.FilterAdapter;
import com.rcw.swiperefreshrecyclerview.OnItemClickListener;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SingleFilterFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SingleFilterFragment extends Fragment implements View.OnClickListener{
    private static final String PARENT_LIST = "PARENT_LIST";

    private TextView tvConfirm,tvReset;

    private Context context;
    private List<String> mValuesList;//存储取值的集合
    private List<String> mParentList;//一级菜单数据集合
    private List<String> mChildList;//一级菜单数据集合
    private ContactAdapter mParentAdapter, mChildAdapter;
    private String gra_name,cls_name;


    public SingleFilterFragment() {
    }

    public static SingleFilterFragment newInstance(List<String> mParentList) {
        SingleFilterFragment fragment = new SingleFilterFragment();
        Bundle args = new Bundle();
        args.putStringArrayList(PARENT_LIST, (ArrayList<String>) mParentList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParentList = getArguments().getStringArrayList(PARENT_LIST);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_single_filter, container, false);
        //存储取值列表的集合
        mValuesList = new ArrayList<>();
        //初始化View
        initView(view);

        return view;
    }

    private void initView(View view) {
        LinearLayout llInfo=(LinearLayout) view.findViewById(R.id.ll_info);
        //展示一级菜单的view
        RecyclerView mRvParent = (RecyclerView) view.findViewById(com.rcw.filterpopup.R.id.rv_parent);
        //展示二级菜单的view
        RecyclerView mRvChild = (RecyclerView) view.findViewById(com.rcw.filterpopup.R.id.rv_child);

        tvConfirm=(TextView)view.findViewById(R.id.tv_confirm);
        tvReset=(TextView)view.findViewById(R.id.tv_reset);
        tvConfirm.setOnClickListener(this);
        tvReset.setOnClickListener(this);
        //默认添加一级菜单对应的二级菜单的第一个值列表
        //mValuesList.addAll(mChildList.get(0));
        // 设置下面三个状态才能监听返回事件  
        llInfo.setFocusable(true);
        llInfo.requestFocus();
        llInfo.setFocusableInTouchMode(true);
        llInfo.setOnKeyListener(backListener);

        mRvParent.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mRvParent.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        mRvChild.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        //mRvChild.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        //创建一级和二级菜单的adapter
        mParentAdapter = new ContactAdapter(context, R.layout.popup_item, mParentList);
        mChildAdapter = new ContactAdapter(context, R.layout.popup_child_item, mValuesList);
        //设置一级菜单的第一个默认选中状态
        mParentAdapter.recordSelectPosition(0);
        mRvParent.setAdapter(mParentAdapter);
        mRvChild.setAdapter(mChildAdapter);

        //父条目点击事件
        mParentAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void setOnClickListener(View view, int position) {
                mParentAdapter.recordSelectPosition(position);
                //清除上次子条目选中的状态
                mChildAdapter.recordSelectPosition(-1);
                mParentAdapter.notifyDataSetChanged();
                mValuesList.clear();
                //模拟加载子类数据
                initChildData(mParentList.get(position));
                gra_name=mParentList.get(position);
                //设置子条目默认选中第一个
                //mRvChild.scrollToPosition(0);
            }

            @Override
            public void setOnLongClickListener(View view, int position) {

            }


        });
        //子条目点击事件
        mChildAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void setOnClickListener(View view, int position) {
                //记录子条目点击的position
                mChildAdapter.recordSelectPosition(position);
                mChildAdapter.notifyDataSetChanged();
                cls_name=mValuesList.get(position);
            }

            @Override
            public void setOnLongClickListener(View view, int position) {

            }
        } );
    }

    private void initChildData(String grade) {
        String gradeCode=grade.substring(0,1);
        mChildList = new ArrayList<>();
        for (int j = 0; j < 15; j++) {
            if (j < 9) {
                mChildList.add(gradeCode + "-0" + (j + 1) + "班");
            } else mChildList.add(gradeCode + "-" + (j + 1) + "班");
        }
        //将子条目添加到取值列表
        mValuesList.addAll(mChildList);
        mChildAdapter.notifyDataSetChanged();
        mChildAdapter.recordSelectPosition(-1);
    }

    private View.OnKeyListener backListener=new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
                EventBus.getDefault().post(new EventMessage(0));
                return true;
            }
            return false;
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_confirm:
                String data=gra_name+cls_name;
                EventBus.getDefault().post(data);
                EventBus.getDefault().post(new EventMessage(0));
                break;
            case R.id.tv_reset:
                mParentAdapter.recordSelectPosition(-1);
                mParentAdapter.notifyDataSetChanged();
                mChildAdapter.recordSelectPosition(-1);
                mChildAdapter.notifyDataSetChanged();
                break;
        }
    }
}
